
from typing import Dict, Any

confidence_filters = {
    "交通事故": 0.96, 
    "道路拥堵": 0.96, 
    "道路施工": 0.90, 
    "抛洒物": 0.70, 
    "道路病害": 0.60, 
    "设施损坏": 0.90, 
    "绿化遮挡": 0.60
}

def filter_by_confidence(data: Dict[str, Any], 
                        #  confidence_filters: Dict[str, float],
                         default_confidence: float = 0.60) -> Dict[str, Any]:
    """
    置信度过滤器
    Args:
        data: 包含事件的JSON数据
        confidence_filters: 事件类型到最小置信度的映射
        default_confidence: 未知类型事件的最小置信度阈值
    Returns:
        过滤后的数据
    """
    if not data.get("is_happen", False):
        return {
            "is_happen": False,
            "events": []
        }
    
    filtered_events = []
    
    for event in data.get("events", []):
        event_type = event.get("event_type", "")
        confidence = event.get("confidence", 0.0)
        
        print(f"检查事件:{event} 类型: {event_type}，置信度: {confidence}")
        # 检查事件类型是否在过滤配置中
        if event_type in confidence_filters:
            min_confidence = confidence_filters[event_type]
            if confidence >= min_confidence:
                filtered_events.append(event)
        elif confidence >= default_confidence:
            filtered_events.append(event)
        
    # 如果过滤后没有事件，将 is_happen 改为 false
    return {
        "is_happen": len(filtered_events) > 0,
        "events": filtered_events
    }
 
 
if __name__ == '__main__':
     
    # 测试数据
    json_data = {'is_happen': True, 'events': [{'confidence': 0.65, 'event_type': '道路拥堵', 'event_level': '中', 'event_time': '2025-11-08 10:44:30', 'location_description': '江苏-S232射宜线-芙蓉大道K324+200-1上行', 'normalized_box': {'x_center': 0.45, 'y_center': 0.55, 'width': 0.15, 'height': 0.3}, 'description': '多车道车辆在交叉口及后方路段持续排队，车辆间距小，通行效率低下，符合拥堵特征'}]}
    # # 过滤配置
    # confidence_filters = {
    #     "交通事故": 0.95, 
    #     "道路拥堵": 0.95, 
    #     "道路施工": 0.90, 
    #     "抛洒物": 0.70, 
    #     "道路病害": 0.70, 
    #     "设施损坏": 0.90, 
    #     "绿化遮挡": 0.70
    # }

    # 执行过滤
    filtered_data = filter_by_confidence(json_data)

    # 打印结果
    print("原始数据:")
    print(f"  is_happen: {json_data['is_happen']}")
    print(f"  事件数量: {len(json_data['events'])}")

    print("\n过滤后数据:")
    print(f"  is_happen: {filtered_data['is_happen']}")
    print(f"  事件数量: {len(filtered_data['events'])}")

    print("\n过滤后的事件:")
    for event in filtered_data["events"]:
        print(f"- {event['event_type']} (置信度: {event['confidence']})")